package com.heisenhub.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.heisenhub.common.db.enums.IsDelEnum;
import com.heisenhub.common.db.service.impl.SuperServiceImpl;
import com.heisenhub.common.redis.utils.RedisUtils;
import com.heisenhub.system.entity.Dept;
import com.heisenhub.system.entity.Menu;
import com.heisenhub.system.enums.SystemEnums;
import com.heisenhub.system.mapper.DeptMapper;
import com.heisenhub.system.query.DeptQuery;
import com.heisenhub.system.service.IDeptService;
import com.heisenhub.system.vo.SysDeptVo;
import com.heisnenhub.common.module.utils.HubObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author sen
 * @since 2023-12-24
 */
@Slf4j
@Service
public class DeptServiceImpl extends SuperServiceImpl<DeptMapper, Dept, DeptQuery> implements IDeptService {

   @Autowired
    RedisUtils redisUtils;
    @Override
    public List<Dept> listAll() {
        return this.list();
    }

    @Override
    public List<Dept> listNotDelAll() {
        QueryWrapper<Dept> wq = new QueryWrapper<>();
        wq.eq(Menu.DEL, IsDelEnum.NORMAL);
        return this.baseMapper.selectList(wq);
    }

    /**
     *  获取 部门树
     * @return
     */
    @Override
    public List deptTree() {
        List<Dept> deptList = listNotDelAll();
        return getDeptTree( deptList);
    }

    private   List getDeptTree( List<Dept> depts){
        List<SysDeptVo> deptVos = new ArrayList<>();
        for (Dept dept : depts) {
            SysDeptVo sysDeptVo = new SysDeptVo();
            sysDeptVo.setDeptId(dept.getId());
            sysDeptVo.setDeptName(dept.getDeptName());
            sysDeptVo.setParentId(dept.getParentId());
            deptVos.add(sysDeptVo);
        }

        /**
         * 树状 部门
         */
        List<SysDeptVo> treeDepts = new ArrayList<>();

        for (SysDeptVo deptVo : deptVos) {
            Long parentId = deptVo.getParentId();
            if(parentId!=null && deptVo.getParentId().intValue() == 0){
                treeDepts.add(deptVo) ;
                continue;
            }
            for (SysDeptVo deptchildVo : deptVos) {
                Long deptId = deptchildVo.getDeptId();
                if(deptId!=null && deptId == parentId ){
                    if (CollectionUtils.isEmpty(deptchildVo.getChildren()))
                        deptchildVo.initChildren();
                    deptchildVo.getChildren().add(deptVo);
                }
            }
        }
        return treeDepts;
    }


    /**
     * 部门 的redis 操作
     */
    @PostConstruct
    public void deptRedisOperate(){
        log.info("【初始化部门】");

        /**
         * 日志的操作
         */
        Map deptNameMap = findAllPidDeptNameMap();
        if( redisUtils.hasKey(SystemEnums.DEPT.redisLogKey())) redisUtils.delForKey(SystemEnums.DEPT.redisLogKey());
        redisUtils.hmset(SystemEnums.DEPT.redisLogKey(),deptNameMap);

    }


    /**
     * 获取所有  部门名称  拼接上级部门
     * @return
     */
    public Map findAllPidDeptNameMap(){

        List<Dept> deptAll = listAll();
        HashMap<String, Object> map = Maps.newHashMap();
        for (Dept sysDept : deptAll) {
            String pidDeptName = getPidDeptName(sysDept.getId(), deptAll);
            map.put(String.valueOf(sysDept.getId()), pidDeptName);
        }
        return map;
    }


    /**
     * 获取 部门名称  拼接上级部门   物联网/开发部/开发一部
     * @return
     */
    public String getPidDeptName(Long id, List<Dept> deptList){
        String deptName="";
        Dept sysDept = deptList.stream().filter(o -> o.getId().equals(id)).findFirst().orElse( null);
        if(HubObjectUtils.isNotEmpty(sysDept)) {
            if(!sysDept.getParentId().equals( 0)){
                deptName =  getPidDeptName(sysDept.getParentId(),deptList);
            }return deptName +"/"+sysDept.getDeptName();
        }
        return "";
    }
}
